package com.wu.store.dao;

import com.wu.store.entity.InstGoods;
import com.wu.store.entity.Order;
import com.wu.store.entity.OrderInst;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderDao {
	@Insert("insert into t_order (uid, name, money, create_time, status) values (#{uid}, #{name}, #{money}, #{createTime}, #{status})")
	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Long.class)
	int add(Order order);

	@Insert("<script>" +
		        "insert into t_order_child (order_id, goods_id, price, count) values " +
		        "<foreach collection='instList' item='inst' separator=','>" +
		        "(#{inst.orderId}, #{inst.goodsId}, #{inst.price}, #{inst.count})" +
		        "</foreach>" +
		        "</script>")
	int batchAddInst(@Param("instList") List<OrderInst> instList);

	@Select("select * from t_order where id = #{id}")
	Order selectById(Long id);

	@Update("<script>" +
		        "update t_order set pay_time = #{payTime}, status = #{status}" +
		        "<where> " +
		        "id = #{id}" +
		        "<if test='uid != null'>" +
		        " and uid = #{uid}" +
		        "</if>" +
		        "</where>" +
		        "</script>")
	int update(Order order);

	@Select("select * from v_inst_goods where order_id = #{orderId}")
	List<InstGoods> selectInstByOrderId(Long orderId);

	@Select("<script>" +
		        "select * from t_order " +
		        "<where>" +
		        "status not in (" +
		        "<foreach collection='statusNotList' item='status' separator=','>" +
		        " #{status}" +
		        "</foreach>)" +
		        "<if test='uid != null'>" +
		        " and uid = #{uid}" +
		        "</if>" +
		        "</where>" +
		        " order by create_time desc" +
		        "</script>")
	List<Order> list(Long uid, List<Long> statusNotList);
}
